package com.fram.service.admin.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.fram.bean.sys.PageBean;
import com.fram.common.canstant.ErrCode;
import com.fram.common.exception.ProException;
import com.fram.common.exception.ProValiDataException;
import com.fram.entity.Keyword;
import com.fram.entity.KeywordAnswer;
import com.fram.mapper.KeywordAnswerMapper;
import com.fram.mapper.KeywordMapper;
import com.fram.service.admin.KeywordsService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
@Service
public class KeywordsServiceImpl implements KeywordsService{
	
	@Autowired
	private KeywordMapper  keywordMapper;
	@Autowired
	private KeywordAnswerMapper  keywordAnswerMapper;

	@Override
	public void add(Keyword request) throws ProException {
		if(keywordMapper.insertSelective(request)==0){
			throw new ProValiDataException(ErrCode.FAIL.getReturnCode(), ErrCode.FAIL.getReturnMsg());
		}
	}

	@Override
	public void answerAdd(KeywordAnswer request) throws ProException {
		if(keywordAnswerMapper.insertSelective(request)==0){
			throw new ProValiDataException(ErrCode.FAIL.getReturnCode(), ErrCode.FAIL.getReturnMsg());
		}
	}

	@Override
	public PageInfo<Keyword> list(PageBean<Keyword> request) throws ProException {
		PageHelper.startPage(request.getPageNum(), request.getPageSize());
		List<Keyword> list = keywordMapper.listForAdmin(request.getParam());
		if(list == null || list.size()==0) {
			throw new ProValiDataException(ErrCode.NO_DATA.getReturnCode(), ErrCode.NO_DATA.getReturnMsg());
		}
		return new PageInfo<>(list);
	}

	@Override
	public PageInfo<KeywordAnswer> ansuerList(PageBean<KeywordAnswer> request) throws ProException {
		PageHelper.startPage(request.getPageNum(), request.getPageSize());
		List<KeywordAnswer> list = keywordAnswerMapper.listForAdmin(request.getParam());
		if (list == null || list.size()==0) {
			throw new ProValiDataException(ErrCode.NO_DATA.getReturnCode(), ErrCode.NO_DATA.getReturnMsg());
		}
		return new PageInfo<>(list);
	}

	@Override
	public Keyword details(Long id) throws ProException {
		Keyword request = keywordMapper.selectByPrimaryKey(id);
		if(request==null) {
			throw new ProValiDataException(ErrCode.NO_DATA.getReturnCode(), ErrCode.NO_DATA.getReturnMsg());
		}
		return request;
	}

	@Override
	public KeywordAnswer answerDetails(Long id) throws ProException {
		KeywordAnswer request = keywordAnswerMapper.selectByPrimaryKey(id);
		if(request == null) {
			throw new ProValiDataException(ErrCode.NO_DATA.getReturnCode(), ErrCode.NO_DATA.getReturnMsg());
		}
		return request;
	}
	
	@Override
	public void update(Keyword request) throws ProException {
		if(keywordMapper.updateByPrimaryKeySelective(request)==0){
			throw new ProValiDataException(ErrCode.FAIL.getReturnCode(), ErrCode.FAIL.getReturnMsg());
		}
	}
	
	@Override
	public void answerUpdate(KeywordAnswer request) throws ProException {
		if(keywordAnswerMapper.updateByPrimaryKeySelective(request)==0) {
			throw new ProValiDataException(ErrCode.FAIL.getReturnCode(), ErrCode.FAIL.getReturnMsg());
		}
	}
	
}
